package com.plian.system.service.yth.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plian.Tools.CollectionUtil;
import com.plian.Tools.DateUtil;
import com.plian.system.entity.ms.QuarterInspect;
import com.plian.system.entity.yth.ms.MonthReport;
import com.plian.system.mapper.yth.ms.MonthReportMapper;
import com.plian.system.service.yth.IMonthReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @Description: TODO
 * @Author: Take-off
 * @Date: 2023/6/9 11:08 AM
 * @Param
 * @return
 * @Version: 1.0
 **/
@Slf4j
@Service
public class MonthReportServiceImpl extends ServiceImpl<MonthReportMapper, MonthReport> implements IMonthReportService {

    private final String YTH_DATASOURCE_TYPE = "3";

    @Transactional(rollbackFor = Exception.class)
//    @DS("slave_1")
    @Override
    public List<QuarterInspect> getAllMonth(Date startTime, Date endTime) {
        List<QuarterInspect> monthPlans = new ArrayList<>();

        List<MonthReport> oldData = list(new LambdaQueryWrapper<MonthReport>()
                .gt(Optional.ofNullable(startTime).isPresent(),MonthReport::getCreateTime,startTime)
                .le(Optional.ofNullable(endTime).isPresent(),MonthReport::getCreateTime,endTime)
        );
        if (CollectionUtil.isNotEmpty(oldData)){
            monthPlans = conversion(oldData);
        }

        return monthPlans;
    }


    public List<QuarterInspect> conversion(List<MonthReport> oldData){
        List<QuarterInspect> quarterInspects = new ArrayList<>();
        ZoneId zone = ZoneId.systemDefault();
        for (MonthReport monthReport:oldData) {
            QuarterInspect inspect = new QuarterInspect();
            inspect.setCode(monthReport.getInvoiceCode());
            inspect.setQuarterDevelopment(monthReport.getWorkPerformance());
            inspect.setCompanyMajorIssues(monthReport.getImportantInformation());
            inspect.setCompanyBudgetImplement(monthReport.getSupervisionSuggestion());
            inspect.setFindMainIssues(monthReport.getMajorProblem());
            inspect.setIssuesRectification(monthReport.getProblemRectification());
            inspect.setDataSource(YTH_DATASOURCE_TYPE);
            inspect.setDataSourceId(monthReport.getId().toString());
            inspect.setStatus(3);
            Optional.ofNullable(monthReport.getReportTime()).ifPresent(m ->{
                inspect.setFillMonth(DateUtil.format(Date.from(m.atStartOfDay().atZone(zone).toInstant()),DateUtil.PATTERN_MONTH));
            });
            Optional.ofNullable(monthReport.getReportOrgId()).ifPresent(m ->{
                inspect.setFillCompanyId(m.toString());
            });
            Optional.ofNullable(monthReport.getReportUserId()).ifPresent(m ->{
                inspect.setCreateUser(m.toString());
            });
            quarterInspects.add(inspect);
        }
        return quarterInspects;
    }

}
